package com.slc.filemanage.controller;

import com.slc.filemanage.config.Common;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

/**
 * Created by sunguangzhu on 2015/3/6.
 */
@Controller
@RequestMapping("/file/")
public class FileController {
    private SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");

    @ResponseBody
    @RequestMapping(value = "upload")
    public String upload(ModelMap model, HttpServletRequest request) throws IOException {
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        String secondDir = multipartRequest.getParameter("secondDir");
        String dir = "";
        if (StringUtils.isNotBlank(secondDir)) {
            dir = Common.uploadDir + secondDir;
            String createDateDir = multipartRequest.getParameter("createDateDir");
            if(StringUtils.isNotBlank(createDateDir)){
                if(Boolean.parseBoolean(createDateDir)){
                    dir+= simpleDateFormat.format(new Date());
                }
            }
            dir+= File.separator;
            File file = new File(dir);
            if (!file.exists()) {
                file.mkdirs();
            }
        }
        Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
        for (String s : fileMap.keySet()) {
            MultipartFile file = fileMap.get(s);
//            String originalFilename = file.getOriginalFilename();
            InputStream inputStream = file.getInputStream();
            FileOutputStream fileOutputStream = new FileOutputStream(dir + s);
            FileCopyUtils.copy(inputStream, fileOutputStream);
            inputStream.close();
            fileOutputStream.close();
        }
        return "success";
    }




    @ResponseBody
    @RequestMapping(value = "download")
    public void download(String path, ModelMap model, HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setContentType("application/octet-stream");
        BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(Common.uploadDir + path));
        //输出流
        BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(response.getOutputStream());
        byte[] buff = new byte[2048];
        int bytesRead;
        while (-1 != (bytesRead = bufferedInputStream.read(buff, 0, buff.length))) {
            bufferedOutputStream.write(buff, 0, bytesRead);
        }
        bufferedOutputStream.flush();
        //关闭流
        bufferedInputStream.close();
        bufferedOutputStream.close();
    }
}
